<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>防抖和节流</title>
    <script src="./js/axios.min.js"></script>
    <script src="./js/vue.js"></script>

</head>

<body>
    <div id="app">

        <input type="text" v-model="i">
        <button @click="add">加一</button>





    </div>

</body>
<script>

    new Vue({
        el: "#app",
        data: {
            i: 1,
            timer : null,

        },
        mounted() {
            // this.num();
        },
        created() {
            // this.num();
            this.debounce(this.num, 1000)
        },
        methods: {

            // 防抖函数封装
            debounce(fn, delay) {
                let timer = null //借助闭包
                return function () {
                    if (timer) {
                        clearTimeout(timer)
                    }
                    timer = setTimeout(fn, delay) // 简化写法
                }
            },


            num() {
     
                let a = this.i

                console.log(a)
                
                this.i = a++;
                console.log(this.i)



                {
                  

                }

        

            },


            add() {


                this.debounce(this.num(),1000)

            }




            // checkPwd() {
            //     if (this.checkAttr("userpwd", "只能由数字字母组成长度6到10位")) {
            //         if (this.userpwd == this.userpwd2) {
            //             this.userpwdtip = "√";
            //             this.userpwdclass = "success";
            //         } else {
            //             this.userpwdtip = "两次输入的密码不一致";
            //             this.userpwdclass = "error";
            //         }
            //     }
            // },


        },
        watch: {
            // i(){

            // }

        }
    })






</script>

</html>